﻿using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.SUPPORT.Components
{
    public partial class Message
    {
        public static Message GetLastest(int topicID)
        {
            const string spName = "p_SUPPORT_Message_GetLastestBy_TopicID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@TopicID", SqlDbType.Int, topicID);

            Message entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Message();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("TopicID"))) entity.TopicID = reader.GetInt32(reader.GetOrdinal("TopicID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Body"))) entity.Body = reader.GetString(reader.GetOrdinal("Body"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }
    }
}
